import 'dart:math' as Math;

import 'package:latlong2/latlong.dart';

class GpsHelper {
  static double pi = 3.14159265358979;
  static double a = 6378245.0;
  static double ee = 0.00669342162296594;

  static double transformLon(double x, double y) {
    double ret = 300.0 +
        x +
        2.0 * y +
        0.1 * x * x +
        0.1 * x * y +
        0.1 * Math.sqrt(x.abs());

    ret +=
        (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * Math.pi)) *
            2.0 /
            3.0;
    ret +=
        (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
    ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) *
        2.0 /
        3.0;
    return ret;
  }

  static double transformLat(double x, double y) {
    double ret = -100.0 +
        2.0 * x +
        3.0 * y +
        0.2 * y * y +
        0.1 * x * y +
        0.2 * Math.sqrt((x.abs()));

    ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) *
        2.0 /
        3.0;
    ret +=
        (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
    ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) *
        2.0 /
        3.0;
    return ret;
  }

  static LatLng transform(double lat, double lon) {
    double dLat = transformLat(lon - 105, lat - 35);
    double dLon = transformLon(lon - 105, lat - 35);
    double radLat = lat / 180.0 * pi;
    double magic = Math.sin(radLat);
    magic = 1 - ee * magic * magic;
    double sqrtMagic = Math.sqrt(magic);
    dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
    dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
    double mgLat = lat + dLat;
    double mgLon = lon + dLon;

    var Gps1 = LatLng(mgLat, mgLon);
    return Gps1;
  }

  static LatLng transformP(LatLng p1) {
    return transform(p1.latitude, p1.longitude);
    // return Gps1;
  }

  static List<LatLng> transformPList(List<LatLng> pList) {
    var pp = <LatLng>[];
    for (var p1 in pList) {
      pp.add(transformP(p1));
    }
    return pp;
    // return Gps1;
  }

  static final double EARTH_RADIUS = 6378.137; //地球半径,单位千米
  static double rad(double d) {
    return d * Math.pi / 180.0;
  }

  /*
     * 
     * @param lat1 第一个纬度
     * @param lng1第一个经度
     * @param lat2第二个纬度
     * @param lng2第二个经度
     * @return 两个经纬度的距离
     */
  static double getDistance(
      double lat1, double lng1, double lat2, double lng2) {
    double radLat1 = rad(lat1);
    double radLat2 = rad(lat2);
    double a = radLat1 - radLat2;
    double b = rad(lng1) - rad(lng2);

    double s = 2 *
        Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
            Math.cos(radLat1) *
                Math.cos(radLat2) *
                Math.pow(Math.sin(b / 2), 2)));
    s = s * EARTH_RADIUS;
    s = s * 1000;
    if (s < 0) s = -1 * s;
    return s;
  }

  static String PointListToString(List<LatLng> PList) {
    String st = "", st2;
    int i;
    for (var item in PList) {
      st2 = item.longitude.toString() + "," + item.latitude.toString();
      st = st + ";" + st2;
    }
    return st;
  }

  static double GetDistance(List<LatLng> PList) {
    double d1 = 0;
    for (int i = 1; i < PList.length; i++) {
      d1 += getDistance(PList[i - 1].latitude, PList[i - 1].longitude,
          PList[i].latitude, PList[i].longitude);
    }
    return d1;
  }

  static List<LatLng> StringToPointList(String st) {
    int i;
    List<LatLng> a = <LatLng>[];

    var st2 = st.split(";");
    try {
      for (var item in st2) {
        var st3 = item.split(",");
        if (st3.length == 2) {
          var p1 = new LatLng(double.parse(st3[1]), double.parse(st3[0]));
          //var p2=GpsHelper.transform(p1.latitude, p1.longitude);
          a.add(p1);
        }
      }
    } catch (e) {
      print(e);
    }
    return a;
  }

  static List<LatLng> StringToPointList2(String st) {
    int i;
    List<LatLng> a = <LatLng>[];

    var st2 = st.split(";");
    try {
      for (var item in st2) {
        var st3 = item.split(",");
        if (st3.length == 2) {
          var p1 = new LatLng(double.parse(st3[1]), double.parse(st3[0]));
          var p2 = GpsHelper.transform(p1.latitude, p1.longitude);
          a.add(p2);
        }
      }
    } catch (e) {
      print(e);
    }
    return a;
  }
}
